package cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.yudao.template.java.service;

import cn.iocoder.yudao.framework.common.genCode.common.StringBuilderHandler;
import cn.iocoder.yudao.module.xiaoBizHandler.dal.dataobject.codegen.XiaoBizHandlerCodegenColumnDO;
import cn.iocoder.yudao.module.xiaoBizHandler.dal.dataobject.codegen.XiaoBizHandlerCodegenTableDO;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.yudao.handler.PathHandler;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.yudao.template.CodeGenTemplate;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.GlobalBinding;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.LocalBinding;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class ServiceImpl implements CodeGenTemplate {

    public String getPath(XiaoBizHandlerCodegenTableDO table, List<XiaoBizHandlerCodegenColumnDO> columns, GlobalBinding globalBinding, LocalBinding localBinding, XiaoBizHandlerCodegenColumnDO primaryColumn) {
        PathHandler pathHandler = PathHandler.builder()
                .table(table)
                .columns(columns)
                .globalBinding(globalBinding)
                .localBinding(localBinding)
                .primaryColumn(primaryColumn)
                .build();
        return pathHandler.javaModuleImplMainFilePath("service/" + table.getBusinessName() + "/" + table.getClassName() + "ServiceImpl");
    }


    public String render(XiaoBizHandlerCodegenTableDO table, List<XiaoBizHandlerCodegenColumnDO> columns, GlobalBinding globalBinding, LocalBinding localBinding, XiaoBizHandlerCodegenColumnDO primaryColumn) {
        StringBuilderHandler code = new StringBuilderHandler();
        code.apAndBl("package " + globalBinding.getBasePackage() + ".module." + table.getModuleName() + ".service." + table.getBusinessName() + ";");
        code.bl();
        code.apAndBl("import org.springframework.stereotype.Service;");
        code.apAndBl("import javax.annotation.Resource;");
        code.apAndBl("import org.springframework.validation.annotation.Validated;");
        code.bl();
        code.apAndBl("import java.util.*;");
        code.apAndBl("import " + globalBinding.getBasePackage() + ".module." + table.getModuleName() + ".controller." + localBinding.getSceneEnum().getBasePackage() + "." + table.getBusinessName() + ".vo.*;");
        code.apAndBl("import " + globalBinding.getBasePackage() + ".module." + table.getModuleName() + ".dal.dataobject." + table.getBusinessName() + "." + table.getClassName() + "DO;");
        code.apAndBl("import " + globalBinding.getPageResultClassName() + ";");
        code.bl();
        code.apAndBl("import " + globalBinding.getBasePackage() + ".module." + table.getModuleName() + ".convert." + table.getBusinessName() + "." + table.getClassName() + "Convert;");
        code.apAndBl("import " + globalBinding.getBasePackage() + ".module." + table.getModuleName() + ".dal.mysql." + table.getBusinessName() + "." + table.getClassName() + "Mapper;");
        code.bl();
        code.apAndBl("import static " + globalBinding.getServiceExceptionUtilClassName() + ".exception;");
        code.apAndBl("import static " + globalBinding.getBasePackage() + ".module." + table.getModuleName() + ".enums.ErrorCodeConstants.*;");
        code.bl();
        code.apAndBl("/**");
        code.apAndBl(" * " + table.getClassComment() + " Service 实现类");
        code.apAndBl(" *");
        code.apAndBl(" * @author " + table.getAuthor() + "");
        code.apAndBl(" */");
        code.apAndBl("@Service");
        code.apAndBl("@Validated");
        code.apAndBl("public class " + table.getClassName() + "ServiceImpl implements " + table.getClassName() + "Service {");
        code.bl();
        code.apAndBl("    @Resource");
        code.apAndBl("    private " + table.getClassName() + "Mapper " + localBinding.getClassNameVar() + "Mapper;");
        code.bl();
        code.apAndBl("    @Override");
        code.apAndBl("    public " + primaryColumn.getJavaType() + " create" + localBinding.getSimpleClassName() + "(" + localBinding.getSceneEnum().getPrefixClass() + "" + table.getClassName() + "CreateReqVO createReqVO) {");
        code.apAndBl("        // 插入");
        code.apAndBl("        " + table.getClassName() + "DO " + localBinding.getClassNameVar() + " = " + table.getClassName() + "Convert.INSTANCE.convert(createReqVO);");
        code.apAndBl("        " + localBinding.getClassNameVar() + "Mapper.insert(" + localBinding.getClassNameVar() + ");");
        code.apAndBl("        // 返回");
        code.apAndBl("        return " + localBinding.getClassNameVar() + ".getId();");
        code.apAndBl("    }");
        code.bl();
        code.apAndBl("    @Override");
        code.apAndBl("    public void update" + localBinding.getSimpleClassName() + "(" + localBinding.getSceneEnum().getPrefixClass() + "" + table.getClassName() + "UpdateReqVO updateReqVO) {");
        code.apAndBl("        // 校验存在");
        code.apAndBl("        this.validate" + localBinding.getSimpleClassName() + "Exists(updateReqVO.getId());");
        code.apAndBl("        // 更新");
        code.apAndBl("        " + table.getClassName() + "DO updateObj = " + table.getClassName() + "Convert.INSTANCE.convert(updateReqVO);");
        code.apAndBl("        " + localBinding.getClassNameVar() + "Mapper.updateById(updateObj);");
        code.apAndBl("    }");
        code.bl();
        code.apAndBl("    @Override");
        code.apAndBl("    public void delete" + localBinding.getSimpleClassName() + "(" + primaryColumn.getJavaType() + " id) {");
        code.apAndBl("        // 校验存在");
        code.apAndBl("        this.validate" + localBinding.getSimpleClassName() + "Exists(id);");
        code.apAndBl("        // 删除");
        code.apAndBl("        " + localBinding.getClassNameVar() + "Mapper.deleteById(id);");
        code.apAndBl("    }");
        code.bl();
        code.apAndBl("    private void validate" + localBinding.getSimpleClassName() + "Exists(" + primaryColumn.getJavaType() + " id) {");
        code.apAndBl("        if (" + localBinding.getClassNameVar() + "Mapper.selectById(id) == null) {");
        code.apAndBl("            throw exception("+ localBinding.getSimpleClassName_underlineCase().toUpperCase() +"_NOT_EXISTS);");
        code.apAndBl("        }");
        code.apAndBl("    }");
        code.bl();
        code.apAndBl("    @Override");
        code.apAndBl("    public " + table.getClassName() + "DO get" + localBinding.getSimpleClassName() + "(" + primaryColumn.getJavaType() + " id) {");
        code.apAndBl("        return " + localBinding.getClassNameVar() + "Mapper.selectById(id);");
        code.apAndBl("    }");
        code.bl();
        code.apAndBl("    @Override");
        code.apAndBl("    public List<" + table.getClassName() + "DO> get" + localBinding.getSimpleClassName() + "List(Collection<" + primaryColumn.getJavaType() + "> ids) {");
        code.apAndBl("        return " + localBinding.getClassNameVar() + "Mapper.selectBatchIds(ids);");
        code.apAndBl("    }");
        code.bl();
        code.apAndBl("    @Override");
        code.apAndBl("    public PageResult<" + table.getClassName() + "DO> get" + localBinding.getSimpleClassName() + "Page(" + localBinding.getSceneEnum().getPrefixClass() + "" + table.getClassName() + "PageReqVO pageReqVO) {");
        code.apAndBl("        return " + localBinding.getClassNameVar() + "Mapper.selectPage(pageReqVO);");
        code.apAndBl("    }");
        code.bl();
        code.apAndBl("    @Override");
        code.apAndBl("    public List<" + table.getClassName() + "DO> get" + localBinding.getSimpleClassName() + "List(" + localBinding.getSceneEnum().getPrefixClass() + "" + table.getClassName() + "ExportReqVO exportReqVO) {");
        code.apAndBl("        return " + localBinding.getClassNameVar() + "Mapper.selectList(exportReqVO);");
        code.apAndBl("    }");
        code.bl();
        code.apAndBl("}");
        code.bl();
        return code.toString();
    }

}